package com.mjk.common.core.jdbc.sphere;


import com.mjk.common.core.jdbc.annotation.ArcSharding;
import com.mjk.common.core.jdbc.sphere.impl.database.ShardingDatabaseAlgorithm;
import com.mjk.common.core.jdbc.sphere.impl.table.CapacityPreciseShardingAlgorithm;
import com.mjk.common.core.jdbc.sphere.impl.table.TimeShardingAlgorithm;
import com.mjk.common.util.ObjectUtils;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.ComplexShardingStrategyConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.ShardingStrategyConfiguration;

import java.time.YearMonth;

/**
 * 
* @author mjk  
* @date 2019年3月5日
 */
public class CapacityAlgorithmUtil {
	/**
	 * 获取shardingruleconfig
     */
	public static ShardingRuleConfiguration getRuleConfig(ArcSharding sharding, String defaultDataSource) {
		ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();	
		if (null==sharding|| ObjectUtils.isEmpty(sharding.key())) {
			return null;
		}
		String shardingColumns = sharding.key();
		if (!ObjectUtils.isEmpty(sharding.subKey())) {
			shardingColumns += "," + sharding.subKey();
		}
		String dataSource = ObjectUtils.isEmpty(sharding.dataSource())?defaultDataSource:sharding.dataSource();
		TableRuleConfiguration tableRule =null;
		if(sharding.strtegy().ordinal() == StrategyEnum.PRECISE.ordinal()) {
			 tableRule = new TableRuleConfiguration(sharding.table(),dataSource+"."+sharding.table()+"_$->{0.."+(sharding.shardingNum()-1)+"}");
			 ShardingStrategyConfiguration tableShardingStrategyConfig = new ComplexShardingStrategyConfiguration(
						shardingColumns, // 分表逻辑
						new CapacityPreciseShardingAlgorithm(sharding));
			 tableRule.setTableShardingStrategyConfig(tableShardingStrategyConfig);
		}else if(sharding.strtegy().ordinal() == StrategyEnum.TIME.ordinal()) {
			//系统重启会创建本年度的表 {2023..2023}$->{01..12} --> {%s..%s}$->{$s..12}
			YearMonth currentYearMonth = YearMonth.now();
			String year = currentYearMonth.getYear()+"";
			String month = currentYearMonth.getMonthValue()+"";
			 tableRule = new TableRuleConfiguration(sharding.table(),dataSource+"."+sharding.table()+"_$->"+String.format(StrategyEnum.TIME.strategy,year,year,month));
			 ComplexShardingStrategyConfiguration tableShardingStrategyConfig = new ComplexShardingStrategyConfiguration(
					 shardingColumns,
					 new TimeShardingAlgorithm(sharding));
			tableRule.setTableShardingStrategyConfig(tableShardingStrategyConfig);
		}
		// 自动一分表规则
		ShardingStrategyConfiguration dataBaseShardingStrategyConfig = new ComplexShardingStrategyConfiguration(
				shardingColumns, // 分库逻辑
				new ShardingDatabaseAlgorithm(dataSource));
		if(null!=tableRule)
			tableRule.setDatabaseShardingStrategyConfig(dataBaseShardingStrategyConfig);
		shardingRuleConfiguration.getTableRuleConfigs().add(tableRule);
		return shardingRuleConfiguration; 
	}
}
